package fr.alma.trajectory.gui;

import java.util.List;

import fr.alma.trajectory.calculating.ICalculator;
import fr.alma.trajectory.commons.ASpaceObject;
import fr.alma.trajectory.commons.Losange;
import fr.alma.trajectory.commons.Rectangle;
import fr.alma.trajectory.commons.Shape;
import fr.alma.trajectory.commons.SpaceObject;
import fr.alma.trajectory.commons.SpacePoint;
import fr.alma.trajectory.commons.Square;
import fr.alma.trajectory.commons.Triangle;
import fr.alma.trajectory.rendering.IViewer;

public class Controller {
	
	/**
	 * Le composant de visualisation
	 */
	private IViewer viewer;
	
	/**
	 * Le composant de calcul de la trajectoire
	 */
	private ICalculator calculator;
	
	public Controller(IViewer viewer, ICalculator calculator) {
		this.viewer = viewer;
		this.calculator = calculator;
	}

	public void run(Shape shape, List<SpacePoint> trajectory){
		
		SpaceObject object = null;
		
		switch(shape){
		case Rectangle :
			object = new Rectangle(new SpacePoint(0., 0., 0.)
								,new SpacePoint(ASpaceObject.RECTANGLE, 0., 0.)
								,new SpacePoint(ASpaceObject.RECTANGLE, ASpaceObject.RECTANGLE/2, 0.)
								,new SpacePoint(0., ASpaceObject.RECTANGLE/2, 0.)
			);
			break;
		case Triangle :
			object = new Triangle(new SpacePoint(0., 0., 0.)
								,new SpacePoint(ASpaceObject.TRIANGLE, 0., 0.)
								,new SpacePoint(ASpaceObject.TRIANGLE/2, ASpaceObject.TRIANGLE, 0.)
			);
			break;
		case Losange :
			object = new Losange(new SpacePoint(0., 0., 0.)
								,new SpacePoint(ASpaceObject.LOSANGE, 0., 0.)
								,new SpacePoint(ASpaceObject.LOSANGE/2, ASpaceObject.LOSANGE, 0.)
								,new SpacePoint(ASpaceObject.LOSANGE, -ASpaceObject.LOSANGE, 0.)
			);
			break;
		case Square :
			object = new Square(new SpacePoint(0., 0., 0.)
								,new SpacePoint(ASpaceObject.SQUARE, 0., 0.)
								,new SpacePoint(ASpaceObject.SQUARE, ASpaceObject.SQUARE, 0.)
								,new SpacePoint(0., ASpaceObject.SQUARE, 0.)
			);
			break;
		}
		
		List<SpaceObject> objects = (List<SpaceObject>) calculator.calculate(object, trajectory);
		System.out.println("Objects : ");
		printObjectsList(objects);
		viewer.setObjectsForScene(objects);
	}
	
	private void printObjectsList(List<SpaceObject> objects){
		for(SpaceObject so: objects){
			System.out.println(so);
		}
	}
	
	
}
